package com.ldu.exam.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ldu.exam.model.TestQuestionModel;
import com.ldu.exam.service.TestQuestionService;

@RequestMapping("/testQuestions")
@RestController
public class TestQuestionController 
{
	@Autowired TestQuestionService service;
	
	@GetMapping()
	public Page<TestQuestionModel> findAll(@PageableDefault(size=20) Pageable page)
	{
		return this.service.findAll(page);
	}
	
	@GetMapping(value="/noPage")
	public List<TestQuestionModel> findAllNoPage(){
		return this.service.findAllNoPage();
	}
	
	@GetMapping(value="/dropList")
	public Object getDropList() {
		return this.service.getDropList();
	}
	
	@GetMapping(value="/{id}/questions")
	public Object findByQuestionId(@PathVariable String id)
	{
		return this.service.findQuestionId(id);
	}
	
	@GetMapping(value="/{id}")
	public Object findById(@PathVariable String id) {
		return this.service.findById(id);
	}
	
	@PostMapping()
	public Object add(@RequestBody TestQuestionModel model) 
	{
		return this.service.add(model);
	}
	
	@DeleteMapping(value="/{id}")
	public Object delete(@PathVariable String id)
	{
		this.service.delete(id);
		return ResponseEntity.noContent().build();
	}
	
	@PutMapping(value="/{id}")
	public Object update(@PathVariable String id, @RequestBody TestQuestionModel model)
	{
		return this.service.update(id, model);
	}
	
	@GetMapping(params="questionTypeId")
	public Page<TestQuestionModel> findByQuestionTypeId(@RequestParam String questionTypeId,@PageableDefault(size=20) Pageable page)
	{
		return this.service.findByQuestionTypeId(questionTypeId,page);
	}
	
	@GetMapping(params="title")
	public Page<TestQuestionModel> findByName(
			@RequestParam String title, 
			 @RequestParam( required=false, defaultValue="0")  int page,
			 @RequestParam( required=false, defaultValue="20") int size
			)
	{
		return this.service.findByName(title, page, size);
	}
	
	@GetMapping(value="/both")
	public Page<TestQuestionModel> findBytilteAndQuestionType(
			@RequestParam String title,
			@RequestParam String questionTypeId,
			@RequestParam( required=false, defaultValue="0")  int page,
			 @RequestParam( required=false, defaultValue="20") int size)
	{
		return this.service.findByBoth(title, questionTypeId, page, size);
	}
}
